﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace KsiazkaKucharska
{
    public partial class zdjecie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id_zdjecia"] != null)
            {
                System.Configuration.Configuration rootWebConfig =	System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/web_config");
			    System.Configuration.ConnectionStringSettings connString;
                connString = rootWebConfig.ConnectionStrings.ConnectionStrings["ksiazka_kucharskaConnectionString"];
                SqlConnection connection = new SqlConnection(connString.ToString());
                string strQuery = "select * from zdjecia_przepisow where id_zdjecia=@id";
                SqlCommand cmd = new SqlCommand(strQuery, connection);
                cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["id_zdjecia"]);

                connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if ( dr.Read()) //yup we found our image
                {
                    Response.Buffer = true;
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.ContentType = dr["typ_zdjecia"].ToString();
                    Response.AddHeader("content-disposition", "attachment;filename="
                    + dr["nazwa_pliku"].ToString());
                    Response.BinaryWrite( (byte[]) dr["zdjecie"] );
                    Response.Flush();
                    Response.End();
                }
                connection.Close();                
            }
        }
    }
}